package com.boot.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 *
 *  跨域过滤器
 * @author meng
 * @version
 * @since 2016年6月19日
 */

@Component
@Slf4j
public class CorsFilter implements Filter {


    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Expose-Headers", "setCookie");
        response.setHeader("setCookie", "we are Cookie");
        //System.out.println("*********************************过滤器被使用**************************");
        HttpServletRequest request= (HttpServletRequest) req;
        log.info ( "请求的地址={}",request.getRequestURL () );
        log.info ( "请求的信息={}",request.getServerName () );
        log.info ( "请求的方式={}",request.getMethod () );
        chain.doFilter(req, res);

    }
    public void init(FilterConfig filterConfig) {}
    public void destroy() {}
}
